home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / importers and exporters / carbon qt graphic import / source / dragcode.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  7.4 KB  |  256 lines

  1. /*
  2.     File:        DragCode.c
  3.     
  4.     Description:Code that works with the Drag Manager to support drag and drop of pictures.
  5.                 Incoming drags only at this point.
  6.  
  7.     Author:        MC
  8.  
  9.     Copyright:     © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
  10.     
  11.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  12.                 ("Apple") in consideration of your agreement to the following terms, and your
  13.                 use, installation, modification or redistribution of this Apple software
  14.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  15.                 please do not use, install, modify or redistribute this Apple software.
  16.  
  17.                 In consideration of your agreement to abide by the following terms, and subject
  18.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  19.                 copyrights in this original Apple software (the "Apple Software"), to use,
  20.                 reproduce, modify and redistribute the Apple Software, with or without
  21.                 modifications, in source and/or binary forms; provided that if you redistribute
  22.                 the Apple Software in its entirety and without modifications, you must retain
  23.                 this notice and the following text and disclaimers in all such redistributions of
  24.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  25.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  26.                 Apple Software without specific prior written permission from Apple.  Except as
  27.                 expressly stated in this notice, no other rights or licenses, express or implied,
  28.                 are granted by Apple herein, including but not limited to any patent rights that
  29.                 may be infringed by your derivative works or by other works in which the Apple
  30.                 Software may be incorporated.
  31.  
  32.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  33.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  34.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  36.                 COMBINATION WITH YOUR PRODUCTS.
  37.  
  38.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  39.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  40.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  41.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  42.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  43.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  44.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45.                 
  46.     Change History (most recent first):
  47.  
  48. */
  49.  
  50. #define TARGET_API_MAC_CARBON 1
  51.  
  52. #include <Drag.h>
  53. #include <QuickTimeComponents.h>
  54. #include <Windows.h>
  55.  
  56. #include "DragCode.h"
  57. #include "GraphicImportDrawCode.h"
  58. #include "Structs.h"
  59. #include "Defines.h"
  60. #include "WindowCode.h"
  61.  
  62. // Globals
  63.         Boolean            canAcceptDrag;
  64. extern    OSErr            gErr;
  65. extern    OSType *        gAcceptedFlavorTypes;
  66. extern    long            gNumGIComponents;
  67.  
  68. static Boolean CanWeAcceptThis (DragReference theDrag) {
  69.     unsigned short            items,
  70.                             index;
  71.     FlavorFlags                theFlags;
  72.     ItemReference            theItem;
  73.     OSErr                    err;
  74.     short                    i;
  75.  
  76.     err = CountDragItems (theDrag, &items);
  77.  
  78.     if (err == noErr) {
  79.         for (index = 1; index <= items; index++) {
  80.             err = GetDragItemReferenceNumber (theDrag, index, &theItem);
  81.  
  82.             if (err == noErr) {
  83.                 i = 0;
  84.                 while (i <= gNumGIComponents) {
  85.                     err = GetFlavorFlags (theDrag, theItem, gAcceptedFlavorTypes[i++], &theFlags);
  86.                     if (err == noErr) {
  87.                         return (true);
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.     }
  93.  
  94.     return (false);
  95. }
  96.  
  97. static Boolean GoodType (OSType theType) {
  98.     Boolean                    isGood;
  99.     short                    i;
  100.  
  101.     isGood = false;
  102.     i = 0;
  103.     do {
  104.         if (theType == gAcceptedFlavorTypes[i++])
  105.             isGood = true;
  106.     } while (isGood == false && i <= gNumGIComponents);
  107.  
  108.     return isGood;
  109. }
  110.  
  111. pascal OSErr MyDefaultTrackingHandler (DragTrackingMessage theMessage, WindowPtr theWindow, void* handlerRefCon, DragReference theDrag) {
  112. #pragma unused (handlerRefCon)
  113.  
  114.     OSErr                        err;
  115.     Rect                        winRect;
  116.  
  117.     err = noErr;
  118.     switch (theMessage) {
  119.         case kDragTrackingEnterHandler:
  120.             break;
  121.  
  122.         case kDragTrackingEnterWindow:
  123.             canAcceptDrag = CanWeAcceptThis (theDrag);
  124.  
  125.             if (canAcceptDrag == true) {
  126.                 RgnHandle    winRgn = NewRgn (),
  127.                             insetWinRgn = NewRgn (),
  128.                             borderRgn = NewRgn ();
  129.  
  130.                 GetWindowPortBounds (theWindow, &winRect);
  131.  
  132.                 RectRgn (winRgn, &winRect);
  133.                 RectRgn (insetWinRgn, &winRect);
  134.  
  135.                 InsetRgn (insetWinRgn, 2, 2);
  136.                 DiffRgn (winRgn, insetWinRgn, borderRgn);
  137.                 EraseRgn (borderRgn);
  138.                 InvalWindowRgn (theWindow, borderRgn);
  139.  
  140.                 ShowDragHilite (theDrag, winRgn, true);
  141.  
  142.                 DisposeRgn (winRgn);
  143.                 DisposeRgn (insetWinRgn);
  144.                 DisposeRgn (borderRgn);                
  145.             }
  146.             break;
  147.  
  148.         case kDragTrackingInWindow:
  149.             if (canAcceptDrag == false) {
  150.                 break;
  151.             } else {
  152.                 // do whatever inside the window (like move an insertion point)
  153.             }
  154.             break;
  155.  
  156.         case kDragTrackingLeaveWindow:
  157.             if (canAcceptDrag == true) {
  158.                 HideDragHilite (theDrag);
  159.             }
  160.  
  161.             canAcceptDrag = false;
  162.             break;
  163.  
  164.         case kDragTrackingLeaveHandler:
  165.             break;
  166.     }
  167.  
  168.     return err;
  169. }
  170.  
  171. pascal OSErr MyDefaultReceiveHandler (WindowPtr theWindow, void* handlerRefCon, DragReference theDrag) {
  172. #pragma unused (handlerRefCon)
  173.  
  174.     unsigned short                items,
  175.                                 index;
  176.     ItemReference                theItem;
  177.     OSType                        theType;
  178.     unsigned short                flavorNum;
  179.     Size                        dataSize;
  180.     WindowInfoHandle            winInfo;
  181.     SInt8                        handleState;                        
  182.  
  183.     if (theWindow != nil) {
  184.         winInfo = (WindowInfoHandle)GetWRefCon (theWindow);
  185.     }
  186.  
  187.     if (winInfo != nil && (**winInfo).sig == kMySig) {
  188.         if (canAcceptDrag == true) {
  189.             gErr = CountDragItems (theDrag, &items);
  190.  
  191.             for (index = 1; index <= items; index++) {
  192.                 gErr = GetDragItemReferenceNumber (theDrag, index, &theItem);
  193.  
  194.                 flavorNum = 1;
  195.  
  196.                 do {
  197.                     gErr = GetFlavorType (theDrag, theItem, flavorNum++, &theType);
  198.                 } while (gErr == noErr && GoodType (theType) == false);
  199.  
  200.                 if (gErr == noErr) {
  201.                     GetFlavorDataSize (theDrag, theItem, theType, &dataSize);
  202.  
  203.                     if (dataSize > 0) {
  204.                         handleState = HGetState ((Handle)winInfo);
  205.                         HLock ((Handle)winInfo);
  206.  
  207.                         ReleaseMemory (winInfo, false);
  208.  
  209.                         (**winInfo).winGraphicType = theType;
  210.                         if (theType == 'PICT') {
  211.                             (**winInfo).winDataHandle = TempNewHandle (dataSize + 512, &gErr);
  212.                         } else {
  213.                             (**winInfo).winDataHandle = TempNewHandle (dataSize, &gErr);
  214.                         }
  215.  
  216.                         if ((**winInfo).winDataHandle != nil) {
  217.                             if (theType == 'PICT') {
  218.                                 gErr = GetFlavorData (theDrag, theItem, theType, *(**winInfo).winDataHandle + 512, &dataSize, 0);
  219.                             } else {
  220.                                 gErr = GetFlavorData (theDrag, theItem, theType, *(**winInfo).winDataHandle, &dataSize, 0);
  221.                             }
  222.                         } else {
  223.                             gErr = cantGetFlavorErr;
  224.                         }
  225.  
  226.                         if (gErr == noErr) {
  227.                             gErr = DrawHandle (theWindow);
  228.                         }
  229.  
  230.                         HSetState ((Handle)winInfo, handleState);
  231.                     }
  232.                 }
  233.             }
  234.         }
  235.     }
  236.  
  237.     return gErr;
  238. }
  239.  
  240. OSErr InitDragManager (void) {
  241.     OSErr                        err;
  242.     DragTrackingHandlerUPP        MyDefaultTrackingHandlerUPP;
  243.     DragReceiveHandlerUPP        MyDefaultReceiveHandlerUPP;
  244.  
  245.     MyDefaultTrackingHandlerUPP = NewDragTrackingHandlerProc (MyDefaultTrackingHandler);
  246.     err = InstallTrackingHandler (MyDefaultTrackingHandlerUPP, 0L, nil);
  247.  
  248.     if (err == noErr) {
  249.         MyDefaultReceiveHandlerUPP = NewDragReceiveHandlerProc (MyDefaultReceiveHandler);
  250.         err = InstallReceiveHandler (MyDefaultReceiveHandlerUPP, 0L, nil);
  251.     }
  252.  
  253.     return (err);
  254. }
  255.  
  256.